package com.gk.uitl;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.eight.vo.OrderPayVo;
import com.gk.config.AliPayConfig;
import com.gk.dao.UserChargeDao;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



import static com.gk.config.AliPayConfig.*;

/**
 * @author: GK
 * @date: 2023/3/9 19:16
 * @version:No1.0
 */
@Controller
@Slf4j
@RequestMapping("/system/payment")
public class GetZFBNative {


//    @Resource
//    StringRedisTemplate redisTemplate;

    @SneakyThrows
    @RequestMapping(value = "/pay", produces = {"text/html;charset=UTF-8"})
    @ResponseBody
    public String aliPay(@RequestBody OrderPayVo tbOrder) {
        //获得初始化的AlipayClient
        AlipayClient alipayClient;
        alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.merchant_private_key, "json", AliPayConfig.charset, alipay_public_key, sign_type);
        //商户订单号，，必填
        String order_number = new String(tbOrder.getOrderNo());
        //付款金额，从前台获取，必填
        String total_amount = new String(tbOrder.getAmount().toString());
        //订单名称，必填
        String subject = new String(tbOrder.getTitle());
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setReturnUrl(return_url);
        JSONObject bizContent = new JSONObject();
        //商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", order_number);
        //支付金额，最小值0.01元
        bizContent.put("total_amount", total_amount);
        //订单标题，不可使用特殊符号
        bizContent.put("subject", subject);
        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            return response.getQrCode();
        } else {
            return null;
        }
    }
}
